// Type of mixin
import { debounce } from 'lodash'
import { getTextWidth } from '@/components/next-ticket-render/components/common'
export default {
  props: {
    params: {
      type: Object,
      default: () => { }
    },
    ruleItem: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      selectDataObj: {},
      interfaceNoDataCount: 0,
      loading: false,
      record: true
    }
  },
  created () {
    this.init()
  },
  computed: {
    getTextWidthOd () {
      return Math.floor(getTextWidth(this.params[this.ruleItem.fields], 12)) > (this.ruleItem.width - 30)
    }
  },
  methods: {
    init () {
      if (this.ruleItem.interfaceUrl) {
        if (this.ruleItem.interfaceUrl === 'startTime/endTime') {
          this.settimeType(this.ruleItem)
        } else if (this.ruleItem.interfaceUrl === 'before/after') {
          this.setcompareType(this.ruleItem)
        } else if (this.ruleItem.interfaceUrl === 'moreThan/lessThan') {
          this.setcompareTypeThen(this.ruleItem)
        } else if (this.ruleItem.interfaceUrl === '>/<') {
          this.setGetgle(this.ruleItem)
        } else if (this.ruleItem.interfaceUrl === 'hours/days/business days') {
          this.setDateTime(this.ruleItem)
        } else {
          this.getSelectDataLists(this.ruleItem)
        }
      }
    },
    getSelectDataLists (item, searchKey) {
      if ((item.type === 'select-more-cascader-api' || item.type === 'select-cascader-api') && item.receiveCascaderId && this.record) {
        this.record = false
        return
      }
      // 4
      this.loading = true
      const url = item.interfaceUrl
      let params = {
        tenantCode: this.$store.state.user.tenantCode
      }
      if (item.params && item.params['type'] && typeof item.params['type'] !== 'undefined') {
        params = {
          ...params,
          ...item.params
        }
      }
      if (searchKey) params[item.searchLabel] = searchKey
      if (item.searchKey) {
        if (item.type === 'select-cascader-api') {
          params = {
            ...params,
            ...item.searchKey
          }
        } else {
          let keyList = Object.keys(item.searchKey)
          keyList.forEach(element => {
            params[element] = item.searchKey[element].join()
          })
        }
      }
      this.$interface.clientReport.getBoardListForBotApi(url, params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            this.resetDataLists(item)
            // return this.$message.error(res.msg)
            return false
          }
          this.loading = false
          if (res.data) {
            if (item.isCustomInputList) {
              this.$store.state.user[`custominputlist-${item.emitCascaderId}`] = res.data
              this.initEmit()
            }
            this.selectDataObj[`${item.fields}ListsOrigin`] = res.data
            if(item.type === 'select-api-toomuch-multiple'){
              if(url.includes('limit')){
                this.setValueData&&this.setValueData(url, params)
              }
            }
            this.selectDataObj = Object.assign({}, this.selectDataObj)
            if (res.data.length <= 0) {
              // this.resetDataLists(item)
            }
          }
        }).catch(() => {
          this.loading = false
          this.resetDataLists(item)
        })
    },
    resetDataLists: debounce(function (item) {
      this.interfaceNoDataCount++
      if (this.interfaceNoDataCount > 5) {
        return false
      }
      this.getSelectDataLists(item)
    }, 2000),
    settimeType (item) {
      var data = [
        { id: 'startTime', name: 'Start Time' },
        { id: 'endTime', name: 'End Time' }
      ]
      this.selectDataObj[`${item.fields}ListsOrigin`] = data
      this.selectDataObj = Object.assign({}, this.selectDataObj)
    },
    setcompareType (item) {
      var data = [
        { id: 'before', name: 'Before' },
        { id: 'after', name: 'After' }
      ]
      this.selectDataObj[`${item.fields}ListsOrigin`] = data
      this.selectDataObj = Object.assign({}, this.selectDataObj)
    },
    setcompareTypeThen (item) {
      var data = [
        { id: 'moreThan', name: 'More Than' },
        { id: 'lessThan', name: 'Less Than' }
      ]
      this.selectDataObj[`${item.fields}ListsOrigin`] = data
      this.selectDataObj = Object.assign({}, this.selectDataObj)
    },
    setGetgle (item) {
      var data = [
        { id: '>', name: '>' },
        { id: '<', name: '<' }
      ]
      this.selectDataObj[`${item.fields}ListsOrigin`] = data
      this.selectDataObj = Object.assign({}, this.selectDataObj)
    },
    setDateTime (item) {
      var data = [
        { id: 'hours', name: 'Hours' },
        { id: 'days', name: 'Days' },
        { id: 'business days', name: 'Business days' }
      ]
      this.selectDataObj[`${item.fields}ListsOrigin`] = data
      this.selectDataObj = Object.assign({}, this.selectDataObj)
    }
  }
}
